﻿using UnityEngine;
using System.Collections;

public class lobbyHandles : MonoBehaviour {
	
	NetworkScript network;
	public UISprite myChar;
	public ReadyRoomScript roomPane;
	public UIPanel menuPane;
	public HostData selectedHostData;
	public int hostID;
	DataScript data;
	public UISprite loobyLabel;
	public UIPanel bgPanel;
	public chatSystem chatWindow;

	public UIButton readyPlayButton;
	public UIButton readyReadyButton;
	public UISprite readyLabel;

	bool playersReady = false;
	
	// Use this for initialization
	void Start () {
		data = GameObject.FindGameObjectWithTag("DataManager").GetComponent<DataScript>();
		network = GameObject.FindGameObjectWithTag("NetworkManager").GetComponent<NetworkScript>();
		if(DataScript.GetCurrentCharacter() < 10)
			myChar.spriteName = "charposes_0" + (DataScript.GetCurrentCharacter() + 2).ToString();
		else
			myChar.spriteName = "charposes_" + (DataScript.GetCurrentCharacter() + 2).ToString();
		//readyLabel
	}
	
	// Update is called once per frame
	void Update () {
	//Debug.Log(Input.mousePosition);
	}
	
	public void startPlaying()
	{
		network.loadLevel(1);
		//LevelManager.Instance.SetMap();
	}

	public void readyUp()
	{
		if(playersReady)
		{
			handlePlayerReady(false);
			networkView.RPC("RPC_playerReadys",RPCMode.Others,false);
		}
		else
		{
			handlePlayerReady(true);
			networkView.RPC("RPC_playerReadys",RPCMode.Others,true);
		}
	}
	
	public void joinARoom(HostData hd, int ID, string mapName)
	{

		network.joinServer(hd);
		hostID = ID;
		data.loadedLevel = mapName;
		data.partnerID = ID;

		data.isServer = false;

		//roomPane.gameObject.SetActive(true);
		//menuPane.gameObject.SetActive(false);
		//roomPane.updatePanels(ID);
		
	}
	
	[RPC] 
	void playerJoins(int ID)
	{
		roomPane.updatePanels(ID);
		data.partnerID = ID;
	}

	[RPC] 
	void RPC_playerReadys(bool isReady)
	{
		handlePlayerReady(isReady);
		readyPlayButton.isEnabled = playersReady;
		readyPlayButton.UpdateColor(playersReady,true);

	}

	void handlePlayerReady(bool isReady)
	{
		playersReady = isReady;
		if(isReady)
			readyLabel.alpha = 1.0f;
		else
			readyLabel.alpha = 0.0f;
	}
	
	void OnConnectedToServer()
	{
		networkView.RPC("playerJoins",RPCMode.Others,DataScript.GetCurrentCharacter());
		this.GetComponent<UIPlayTween>().Play(true);
		loobyLabel.enabled = false;
		chatWindow.enabled = true;
		handlePlayerReady(false);
		readyPlayButton.gameObject.SetActive(false);
		readyReadyButton.gameObject.SetActive(true);
		//menuPane.Refresh();
        UIPlayTween[] myTweens = this.GetComponents<UIPlayTween>();
		//menuPane.gameObject.SetActive(false);
		//Debug.Log(mysTweens.Length);
		//for(int i = 0; i < myTweens.Length; i++)
		//{
		//	myTweens[i].Play(true);
		//}
		roomPane.partnerUpdate(hostID);
		menuPane.Refresh();
		bgPanel.Refresh();
		//Debug.Log("SERVERCONNECTED");
	}
	
	void OnPlayerDisconnected(NetworkPlayer player)
	{
		handlePlayerReady(false);
		roomPane.ExitPanelUpdate();
	}
	
	public void leaveRoom()
	{
		Network.Disconnect();
		chatWindow.allMesseges.Clear();
		chatWindow.chatMessage = "";
		
		chatWindow.enabled = false;
		loobyLabel.enabled = true;
		menuPane.Refresh();
		bgPanel.Refresh();
		handlePlayerReady(false);
		//chatGUI.gameObject.SetActive(false);
	}

	void OnServerInitialized()
	{
		readyPlayButton.gameObject.SetActive(true);
		readyPlayButton.isEnabled = false;
		readyPlayButton.UpdateColor(false,true);
		readyReadyButton.gameObject.SetActive(false);
		handlePlayerReady(false);
	}
	
	public void startShop()
	{
		Application.LoadLevel("ShopScene");
	}
	
	public void startLevelEditor()
	{
		Application.LoadLevel("LevelEditor");
	}
}
